﻿using Restaurant.Client.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Restaurant.Client.Framework;
using Restaurant.Model;
using Restaurant.Common;
using Restaurant.Client.Framework.Extension;
using Newtonsoft.Json;
using System.Net.Http;

namespace Restaurant.Client.Pages
{
    /// <summary>
    /// p_Settings_Table.xaml 的交互逻辑
    /// </summary>
    public partial class p_Settings_Table : Page
    {
        public p_Settings_Table()
        {
            InitializeComponent();
            GetTable();
        }


        /// <summary>
        /// 获取全部桌子
        /// </summary>
        private async void GetTable()
        {
            var s = new Dictionary<string, object>();
            s.Add("subComanyId", Global.SubCompanyId);
            var r = await APIHelper.TryGetAsync<OperationResult>("Table/GetAllBySubComanyId", s);
            var tables = r.GetListAppData<CompanyTable>();

            if (null == tables || tables.Item2 < 1)
                return;

            var count = tables.Item2;
            foreach (var t in tables.Item1)
            {
                //界面显示
                var ut = new u_Table(t);

                ut.Delete += T_Delete;
                wp_Main.Children.Add(ut);
            }

        }

        #region 添加

        /// <summary>
        /// 添加桌子
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        private void dh_Table_DialogClosing(object sender, MaterialDesignThemes.Wpf.DialogClosingEventArgs eventArgs)
        {
            try
            {
                //取消判断
                if (!Equals(eventArgs.Parameter, true)) return;


                AddTable();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                txt_Add_Number.Text = "";
                txt_Add_Count.Text = "";
                txt_Add_Remarks.Text = "";
            }

        }

        /// <summary>
        /// 添加桌子
        /// </summary>
        private async void AddTable()
        {  //保存数据
            var table = new CompanyTable();
            table.SubCompany_Id = Global.SubCompanyId;
            table.TableNum = txt_Add_Number.Text.Trim();
            table.PeopleCount = string.IsNullOrEmpty(txt_Add_Count.Text) ? 0 : Convert.ToInt32(txt_Add_Count.Text);
            table.Remarks = txt_Add_Remarks.Text.Trim();
            table.Operator = Global.Oprator;

            var s = new Dictionary<string, object>();
            //s.Add( JsonConvert.SerializeObject(table) );
            var r = await APIHelper.TryPostAsync<OperationResult>("Table/Add", table);


            if ("成功" == r.Message)
            {     //界面显示
                var t = new u_Table(r.GetAppData<CompanyTable>());
                t.Delete += T_Delete;
                wp_Main.Children.Add(t);

            }




        }

        #endregion


        #region 删除


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        private void T_Delete(u_Table sender)
        {
            Delte(sender);
        }

        private async void Delte(u_Table u)
        {
            //删除数据
            var s = new Dictionary<string, object>();
            s.Add("id", u.CompanyTable.Id);
            var r = await APIHelper.TryDeleteAsync<OperationResult>("Table/Delete", s);

            if ("成功" == r.Message) 
                wp_Main.Children.Remove(u);
        }

        #endregion
    }
}
